



//
//  NStarPopView.m
//  PPShareView
//
//  Created by macfai on 2018/9/29.
//  Copyright © 2018年 pengpeng. All rights reserved.
//

#import "NStarPopView.h"
#import "AXRatingView.h"
#import "UIView+Sizes.h"
#define kSCREENWIDTH     [UIScreen mainScreen].bounds.size.width
#define kSCREENHEIGHT    [UIScreen mainScreen].bounds.size.height
#define kViewHeight 260
#define kFont 30

@interface NStarPopView()

@property(nonatomic,strong)UIView *bottomBgView;

@property(nonatomic,assign)NSInteger starNum;

@property(nonatomic,strong)NSMutableDictionary *starDict;
@end


@implementation NStarPopView

- (instancetype)initWithFrame:(CGRect)frame{
    self=[super initWithFrame:frame];
    if (self) {
        self.frame = [UIScreen mainScreen].bounds;
        self.backgroundColor = [[UIColor lightGrayColor]colorWithAlphaComponent:0.5];
        self.starDict = [NSMutableDictionary dictionary];
        [self setUI];
    }
    return self;
}

- (void)tapView:(UITapGestureRecognizer *)tap
{
    [self dismiss];
}

- (void)setUI {
    
    ///触摸view
    UIView *touchView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kSCREENWIDTH, kSCREENHEIGHT-kViewHeight)];
    touchView.backgroundColor = [UIColor colorWithWhite:0.3 alpha:0.3];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapView:)];
    [touchView addGestureRecognizer:tap];
    [self addSubview:touchView];
    
    self.bottomBgView = [[UIView alloc]initWithFrame:CGRectMake(0, kSCREENHEIGHT, kSCREENWIDTH, kViewHeight)];
    self.bottomBgView.backgroundColor = [UIColor colorWithRed:240.0/255.0 green:240.0/255.0 blue:240.0/255.0 alpha:1.0];
    [self addSubview:self.bottomBgView];
    
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 10, kSCREENWIDTH, 18)];
    label.centerX = self.centerX;
    label.text =@"满意度评分";
    label.textColor = [UIColor darkGrayColor];
    label.font = [UIFont systemFontOfSize:19];
//    label.backgroundColor = [UIColor magentaColor];
    
    label.textAlignment = NSTextAlignmentCenter;
    [self.bottomBgView addSubview:label];
    
    //提交按钮
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = CGRectMake(0, 200, 300, 50);
    btn.centerX = self.centerX;
    [btn addTarget:self action:@selector(tapActoin:) forControlEvents:UIControlEventTouchUpInside];
    btn.layer.cornerRadius = 8.f;
    btn.layer.masksToBounds = YES;
    [btn setTitle:@"提交" forState:UIControlStateNormal];
    btn.backgroundColor = NColorFrom16RGB(0x23b7e5);
    [self.bottomBgView addSubview:btn];
    
    //整体满意度label
    UILabel *label1 = [[UILabel alloc]initWithFrame:CGRectMake(55, label.bottom+20, 105, 18)];
    label1.text =@"整体满意度";
    label1.textColor = [UIColor darkGrayColor];
    label1.font = [UIFont systemFontOfSize:17];
//    label1.backgroundColor = [UIColor magentaColor];
    
    label1.textAlignment = NSTextAlignmentLeft;
    [self.bottomBgView addSubview:label1];
    
    AXRatingView *ratingView = [[AXRatingView alloc] initWithFrame:CGRectMake(label1.right+50, label.bottom+15, 105, 30)];
//    ratingView.backgroundColor = [UIColor magentaColor];
    ratingView.markFont = [UIFont systemFontOfSize:kFont];
    [ratingView setStepInterval:1.0];
    ratingView.tag  = 101;
    ///实时监听
    [ratingView addTarget:self action:@selector(changeRate:) forControlEvents:UIControlEventValueChanged];
    [self.bottomBgView addSubview:ratingView];
    
    
    //客服答复速度label
    UILabel *label2 = [[UILabel alloc]initWithFrame:CGRectMake(55, label1.bottom+20, 105, 18)];
    label2.text =@"客服答复速度";
    label2.textColor = [UIColor darkGrayColor];
    label2.font = [UIFont systemFontOfSize:17];
//    label2.backgroundColor = [UIColor magentaColor];
    
    label2.textAlignment = NSTextAlignmentLeft;
    [self.bottomBgView addSubview:label2];
    
    AXRatingView *ratingView2 = [[AXRatingView alloc] initWithFrame:CGRectMake(label1.right+50, label1.bottom+15, 100, 30)];
//    ratingView2.backgroundColor = [UIColor magentaColor];
    ratingView2.markFont = [UIFont systemFontOfSize:kFont];
    [ratingView2 setStepInterval:1.0];
    ratingView2.tag  = 201;
    [ratingView2 addTarget:self action:@selector(changeRate:) forControlEvents:UIControlEventValueChanged];
    [self.bottomBgView addSubview:ratingView2];
    
    
    //客服工作态度 label
    UILabel *label3 = [[UILabel alloc]initWithFrame:CGRectMake(55, label2.bottom+20, 105, 18)];
    label3.text =@"客服工作态度";
    label3.textColor = [UIColor darkGrayColor];
    label3.font = [UIFont systemFontOfSize:17];
//    label3.backgroundColor = [UIColor magentaColor];
    
    label3.textAlignment = NSTextAlignmentLeft;
    [self.bottomBgView addSubview:label3];
    
    AXRatingView *ratingView3 = [[AXRatingView alloc] initWithFrame:CGRectMake(label3.right+50, label2.bottom+15, 100, 30)];
//    ratingView3.backgroundColor = [UIColor magentaColor];
    ratingView3.markFont = [UIFont systemFontOfSize:kFont];
    [ratingView3 setStepInterval:1.0];
    ratingView3.tag  = 301;
    [ratingView3 addTarget:self action:@selector(changeRate:) forControlEvents:UIControlEventValueChanged];
    [self.bottomBgView addSubview:ratingView3];
    
    
    //委办局答复 label
    UILabel *label4 = [[UILabel alloc]initWithFrame:CGRectMake(55, label3.bottom+20, 105, 18)];
    label4.text =@"委办局答复";
    label4.textColor = [UIColor darkGrayColor];
    label4.font = [UIFont systemFontOfSize:17];
//    label4.backgroundColor = [UIColor magentaColor];
    
    label4.textAlignment = NSTextAlignmentLeft;
    [self.bottomBgView addSubview:label4];
    
    AXRatingView *ratingView4 = [[AXRatingView alloc] initWithFrame:CGRectMake(label1.right+50, label3.bottom+15, 100, 30)];
//    ratingView4.backgroundColor = [UIColor magentaColor];
    ratingView4.markFont = [UIFont systemFontOfSize:kFont];
//    ratingView4.markImage = [UIImage imageNamed:@"star"];
    [ratingView4 setStepInterval:1.0];
    ratingView4.tag  = 401;
    [ratingView4 addTarget:self action:@selector(changeRate:) forControlEvents:UIControlEventValueChanged];
    [self.bottomBgView addSubview:ratingView4];
    
}


-(void)setStarBlcock:(StarSelectedBlock)starBlcock{
    _starBlcock = starBlcock;
}

- (void)changeRate:(AXRatingView *)sender
{
    
//    self.starNum = sender.value;
//    if (sender.tag == 101) {
//
//    }
    NSInteger num=sender.value;
    [self.starDict setObject:[NSString stringWithFormat:@"%ld",num] forKey:[NSString stringWithFormat:@"%ld",(long)sender.tag]];
//     self.starBlcock(sender.tag, sender.value);


    
    
}
-(void)setButtonClick:(ButtonClick)buttonClick{
    _buttonClick = buttonClick;
}

- (void)tapActoin:(UIButton *)btn
{

//    for (NSString *str in [self.starDict allKeys]) {
//        self.starNum += [[self.starDict objectForKey:str] integerValue];
//    }
    if ([[self.starDict allKeys] count] < 4) {
        [AFToast showText:@"请选择所有评分"];
        return;
    }
    if (self.buttonClick) {
        self.buttonClick(btn,self.starDict);
    }
    [self dismiss];
    
}


-(void)show{
    
    [[UIApplication sharedApplication].keyWindow addSubview:self];
    [UIView animateWithDuration:.3 animations:^{
        if (kSCREENWIDTH>320) {
            self.bottomBgView.transform = CGAffineTransformMakeTranslation(0,  - kViewHeight);
        }else{
            self.bottomBgView.transform=  CGAffineTransformMakeTranslation(0, -180);
        }
        
    }];
}

-(void)dismiss{
    
    [UIView animateWithDuration:0.3 animations:^{
        
        self.bottomBgView.transform = CGAffineTransformIdentity;
        
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
    
}


//- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
//{
//    [self dismiss];
//}


@end
